﻿namespace TriangleGames.TrainResEditor
{
    using System;
    using UnityEditor;
    using UnityEngine;

    internal class MainToolbar
    {
        private static void DoFsmSelectorGUI()
        {
            GUIContent content = new GUIContent((FsmEditor.SelectedFsmGameObject == null) ? "空" : FsmEditor.SelectedFsmGameObject.name, "选择对象");
            if (GUILayout.Button(content, EditorStyles.toolbarDropDown, new GUILayoutOption[] { GUILayout.MinWidth(100f) }))
            {
                FsmEditorGUILayout.GenerateFsmGameObjectSelectionMenu(true).ShowAsContext();
            }
            GUIContent content2 = new GUIContent((FsmEditor.SelectedFsm == null) ? "" : FsmEditor.SelectedFsm.Name, "选择状态机");
            if (GUILayout.Button(content2, EditorStyles.toolbarDropDown, new GUILayoutOption[] { GUILayout.MinWidth(100f) }))
            {
                FsmEditorGUILayout.GenerateGameObjectFsmSelectionMenu().ShowAsContext();
            }
            FsmEditorSettings.LockGraphView = GUILayout.Toggle(FsmEditorSettings.LockGraphView, new GUIContent("锁定", "保持状态机选定"), EditorStyles.toolbarButton, new GUILayoutOption[0]);
            if (GUI.changed)
            {
                FsmEditorSettings.SaveSettings();
            }
        }

        private static void DoMainMenu()
        {
            if (GUILayout.Button(FsmEditorStyles.FsmBrowserButton, EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
            {
                GenericMenu menu = new GenericMenu();
                menu.AddItem(new GUIContent("状态机浏览器"), false, new GenericMenu.MenuFunction(FsmEditor.OpenFsmSelectorWindow));
                menu.AddItem(new GUIContent("状态浏览器"), false, new GenericMenu.MenuFunction(FsmEditor.OpenStateSelectorWindow));
                menu.AddItem(new GUIContent("模板浏览器"), false, new GenericMenu.MenuFunction(FsmEditor.OpenFsmTemplateWindow));
                menu.AddItem(new GUIContent("工具窗口"), false, new GenericMenu.MenuFunction(FsmEditor.OpenToolWindow));
                menu.AddItem(new GUIContent("动作浏览器"), false, new GenericMenu.MenuFunction(FsmEditor.OpenActionWindow));
                menu.AddItem(new GUIContent("全局变量"), false, new GenericMenu.MenuFunction(FsmEditor.OpenGlobalVariablesWindow));
                menu.AddItem(new GUIContent("事件浏览器"), false, new GenericMenu.MenuFunction(FsmEditor.OpenGlobalEventsWindow));
                menu.AddItem(new GUIContent("日志窗口"), false, new GenericMenu.MenuFunction(FsmEditor.OpenFsmLogWindow));
                menu.AddItem(new GUIContent("控制台"), false, new GenericMenu.MenuFunction(FsmEditor.OpenReportWindow));
                menu.ShowAsContext();
            }
        }

        private static void DoPrefabTypeGUI()
        {
            if (FsmEditor.SelectedFsmGameObject != null)
            {
                bool isModifiedPrefabInstance = FsmEditor.SelectedFsm.IsModifiedPrefabInstance;
                string str = "";
                if (isModifiedPrefabInstance)
                {
                    str = " (修改)";
                }
                switch (FsmEditor.Selection.ActiveFsmPrefabType)
                {
                    case PrefabType.None:
                        if (GUILayout.Button(new GUIContent("选择", "选择对象"), EditorStyles.toolbarButton, new GUILayoutOption[0]))
                        {
                            FsmEditor.SelectFsmGameObject();
                        }
                        return;

                    case PrefabType.Prefab:
                        if (GUILayout.Button("预制件", EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
                        {
                            GenericMenu menu = new GenericMenu();
                            menu.AddItem(new GUIContent("选择预制件"), false, new GenericMenu.MenuFunction(FsmEditor.SelectFsmGameObject));
                            menu.AddItem(new GUIContent("创建实例"), false, new GenericMenu.MenuFunction(FsmEditor.InstantiatePrefab));
                            menu.ShowAsContext();
                        }
                        return;

                    case PrefabType.ModelPrefab:
                        if (GUILayout.Button("模型预制件", EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
                        {
                            GenericMenu menu3 = new GenericMenu();
                            menu3.AddItem(new GUIContent("选择对象"), false, new GenericMenu.MenuFunction(FsmEditor.SelectFsmGameObject));
                            menu3.AddItem(new GUIContent("创建实例"), false, new GenericMenu.MenuFunction(FsmEditor.InstantiatePrefab));
                            menu3.ShowAsContext();
                        }
                        return;

                    case PrefabType.PrefabInstance:
                        if (GUILayout.Button("预制件实例" + str, EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
                        {
                            GenericMenu menu2 = new GenericMenu();
                            menu2.AddItem(new GUIContent("选择对象"), false, new GenericMenu.MenuFunction(FsmEditor.SelectFsmGameObject));
                            if (!isModifiedPrefabInstance)
                            {
                                menu2.AddDisabledItem(new GUIContent("还原到预制件"));
                            }
                            else
                            {
                                menu2.AddItem(new GUIContent("还原到预制件"), false, new GenericMenu.MenuFunction(FsmEditor.ResetToPrefabState));
                            }
                            menu2.AddItem(new GUIContent("选择预制件"), false, new GenericMenu.MenuFunction(FsmEditor.SelectPrefabParent));
                            menu2.ShowAsContext();
                        }
                        return;

                    case PrefabType.ModelPrefabInstance:
                        if (GUILayout.Button("Model Prefab Instance", EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
                        {
                            GenericMenu menu4 = new GenericMenu();
                            menu4.AddItem(new GUIContent("选择对象"), false, new GenericMenu.MenuFunction(FsmEditor.SelectFsmGameObject));
                            if (!isModifiedPrefabInstance)
                            {
                                menu4.AddDisabledItem(new GUIContent("还原到预制件"));
                            }
                            else
                            {
                                menu4.AddItem(new GUIContent("还原到预制件"), false, new GenericMenu.MenuFunction(FsmEditor.ResetToPrefabState));
                            }
                            menu4.AddItem(new GUIContent("选择预制件父对象"), false, new GenericMenu.MenuFunction(FsmEditor.SelectPrefabParent));
                            menu4.ShowAsContext();
                        }
                        return;

                    case PrefabType.MissingPrefabInstance:
                        return;

                    case PrefabType.DisconnectedPrefabInstance:
                        if (GUILayout.Button("预制件实例 (断开连接)", EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
                        {
                            GenericMenu menu5 = new GenericMenu();
                            menu5.AddItem(new GUIContent("选择对象"), false, new GenericMenu.MenuFunction(FsmEditor.SelectFsmGameObject));
                            menu5.AddItem(new GUIContent("重新连接到预制件"), false, new GenericMenu.MenuFunction(FsmEditor.ReconnectToLastPrefab));
                            menu5.ShowAsContext();
                        }
                        return;

                    case PrefabType.DisconnectedModelPrefabInstance:
                        if (GUILayout.Button("模型预制件实例 (断开连接)", EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
                        {
                            GenericMenu menu6 = new GenericMenu();
                            menu6.AddItem(new GUIContent("选择对象"), false, new GenericMenu.MenuFunction(FsmEditor.SelectFsmGameObject));
                            menu6.AddItem(new GUIContent("重新连接到预制件"), false, new GenericMenu.MenuFunction(FsmEditor.ReconnectToLastPrefab));
                            menu6.ShowAsContext();
                        }
                        return;
                }
            }
        }

        public void OnGUI()
        {
            float width = FsmEditor.Window.position.width - 350f;
            EditorGUILayout.BeginHorizontal(EditorStyles.toolbar, new GUILayoutOption[] { GUILayout.Width(width) });
            DoMainMenu();
            DoFsmSelectorGUI();
            DoPrefabTypeGUI();
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
        }
    }
}

